package com.lipeng.service.impl;

import com.lipeng.bean.Game;
import com.lipeng.bean.Tickets;
import com.lipeng.mapper.GameMapper;
import com.lipeng.mapper.TicketsMapper;
import com.lipeng.service.GameService;
import com.lipeng.utils.AjaxResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/8.
 */
@Service
public class GameServiceImpl implements GameService {
    @Resource
    private GameMapper gameMapper;
    @Resource
    private TicketsMapper ticketsMapper;


    @Override
    public Game selectGameById(Integer gid) {
        return gameMapper.selectByPrimaryKey(gid);
    }

    @Override
    public List<Game> selectAllGame() {
        return gameMapper.selectAllGame();
    }

    @Override
    public int insertGame(Game game) {
        return gameMapper.insert(game);
    }

    @Override
    public int updateGame(Game game) {
        return gameMapper.updateByPrimaryKeySelective(game);
    }

    @Override
    public int deleteGameById(Integer gid) {
        return gameMapper.deleteByPrimaryKey(gid);
    }

    @Override
    public List<Game> selectGameByName(String name) {
        return gameMapper.selectGameByName(name);
    }

    @Override
    public AjaxResult buyTickets(Integer uid, Integer gid, Integer count) {
        AjaxResult result = new AjaxResult();
        Tickets tickets = new Tickets();
        tickets.setUid(uid);
        tickets.setGid(gid);
        tickets.setCount(count);
        tickets.setStatus(0);
        int num = ticketsMapper.insert(tickets);
        if (num<1){
            result.setCode("0");
            result.setMessage("添加失败");
        }
        result.setCode("1");
        result.setMessage("添加成功");
        result.setData(tickets.getTid());
        return result;
    }

    @Override
    public AjaxResult payTickets(Integer tid) {
        AjaxResult result = new AjaxResult();
        int num = ticketsMapper.payTickets(tid);
        if (num<1){
            result.setCode("0");
            result.setMessage("付款失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("付款成功");
        return result;
    }

    @Override
    public AjaxResult delTickets(Integer tid) {
        AjaxResult result = new AjaxResult();
        Integer num = ticketsMapper.deleteByPrimaryKey(tid);
        if (num<1){
            result.setCode("0");
            result.setMessage("取消失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("取消成功");
        return result;
    }

    @Override
    public List<Tickets> selectTicketsByUid(Integer uid) {
        return ticketsMapper.selectTicketsByUid(uid);
    }

    @Override
    public Tickets selectTicketsByTid(Integer tid) {
        return ticketsMapper.selectByPrimaryKey(tid);
    }
}
